package presentation;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.io.File;
import java.util.Arrays;
import java.util.List;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.leapmotion.leap.Gesture;

import data.LeapMotionSimonSays;
import edu.cmu.sphinx.speech.recognition.SpeechRec;


public class MainScreen {
	
	static JFrame frame;
	
	static int WIDTH = 800;
	static int HEIGHT = 600;
	static int NUMBER_OF_PLAYERS = 2;
	final static String MENUPANEL = "Menu";
    final static String INSTRUCTIONPANEL = "Instructions";
    final static String GESTURESPANEL = "Possible Gestures";
    final static String PLAYPANEL = "Play";
    final static String NUMBEROFPLAYERSPANEL = "Number of players";
	
    public static CardLayout cardLayout;
    public GridLayout gridLayout;
    public static SpeechRec speak;
    public static LeapMotionSimonSays SiSa;
    public static JLabel speechLabel;
    
    //gestures that will be recignized in the game
    private static List<Gesture.Type> gestureTypes = Arrays.asList(Gesture.Type.TYPE_CIRCLE , Gesture.Type.TYPE_KEY_TAP, Gesture.Type.TYPE_SCREEN_TAP, Gesture.Type.TYPE_SWIPE);
    
    //Create the "cards".
    private JPanel menuCard = new MenuPanel();
    private JPanel instructionsCard = new InstructionsPanel();
    private JPanel gesturesCard = new GesturesPanel();
    private static JPanel playCard = new PlayPanel();
    private static JPanel NumberOfPlayersCard = new NumberOfPlayersPanel();

    // JPanel to hold the "cards" and to use the CardLayout:
    static JPanel cardContainer;
    
    //background
    public static ImageIcon background =
            new ImageIcon("gamebackground1.jpg");
    
   
    public void addComponentToPane(Container pane) {
    	
        //Label to indicate the words a user can use to switch screens
        speak = new SpeechRec();
        speechLabel = new JLabel("Say: (Play | Instructions | Gestures | Choose number of players | Exit game)");
        pane.add(speechLabel, BorderLayout.PAGE_START);
  

        //Create the panel that contains the "cards".
        cardLayout = new CardLayout();
        cardContainer = new JPanel(cardLayout);
        cardContainer.setOpaque (false);
        cardContainer.setBackground (Color.BLACK);
        
    	pane.add(cardContainer, BorderLayout.CENTER);
     
        cardContainer.add(menuCard, MENUPANEL);
        cardContainer.add(instructionsCard, INSTRUCTIONPANEL);
        cardContainer.add(gesturesCard, GESTURESPANEL);
        cardContainer.add(playCard, PLAYPANEL);
        cardContainer.add(NumberOfPlayersCard, NUMBEROFPLAYERSPANEL);
    }

    //Speech recognition and according actions
    public static int changeItem(){

    	int index = speak.giveWord();
    	switch(index){
    	case 1:  index = 1;
    		frame.setVisible(false);
    		frame.dispose();
    		break;
    	case 2:  index = 2;
    		cardLayout.show(cardContainer, MENUPANEL);
    		speechLabel.setText("Say: (Play | Instructions | Gestures | Choose number of players | Exit game)");
    		break;
    	case 3: index = 3;
    		cardLayout.show(cardContainer, INSTRUCTIONPANEL);
    		speechLabel.setText("Say: Back");
    		break;
    	case 4: index = 4;
    		cardLayout.show(cardContainer, GESTURESPANEL);
      		speechLabel.setText("Say: Back");
    		break;
    	case 5: index = 5;
			cardLayout.show(cardContainer, PLAYPANEL);
			speechLabel.setText("Say: Back");
			SiSa = new LeapMotionSimonSays(gestureTypes);
			SiSa.start(NUMBER_OF_PLAYERS);
			SiSa.stopGame();
			break;
    	case 6: index = 6;
			cardLayout.show(cardContainer, NUMBEROFPLAYERSPANEL);
			speechLabel.setText("Say: 'Back' or a number between '1 and 5' to change the number of players");
			while(((NumberOfPlayersPanel) NumberOfPlayersCard).changeNumber() != 2){
	    	}
			NUMBER_OF_PLAYERS = NumberOfPlayersPanel.getNumber();
			System.out.println("number of players is: " + NUMBER_OF_PLAYERS);
			cardLayout.show(cardContainer, MENUPANEL);
    		speechLabel.setText("Say: (Play | Instructions | Gestures | Choose number of players | Exit game)");
			break;
    	}
    	return index;
    }
    
    
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
    
        //Create and set up the window.
        frame = new JFrame("SimonSays");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        MainScreen demo = new MainScreen();
        demo.addComponentToPane(frame.getContentPane());
        
        frame.setSize(WIDTH,HEIGHT);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        
        
        //Display the window.
        frame.pack();
        frame.setVisible(true);
        
        
    }
    
    //Switch the gesture image
    public static void ChangeGesture(int gest, int player, int score, boolean newGesture, int indexGesture){
    	cardContainer.remove(playCard);
    	((PlayPanel) playCard).changeImage(gest, player, score, newGesture, indexGesture);
    	cardContainer.add(playCard, PLAYPANEL);
    	cardLayout.show(cardContainer, PLAYPANEL);
    }
    
    //show a pop up message, when a player loses.
    public static void popupLost(int player){
    	JOptionPane popup = new JOptionPane();
    	playSound();
    	popup.showMessageDialog(null, "Player " + (player+1) + " lost the game");
    }
    
    
    //play the "loser" sound
    public static void playSound(){
    	try {
            Clip clip = AudioSystem.getClip();
            AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File("fail2.wav"));
            clip.open(inputStream);
            clip.start(); 
        } catch (Exception e) {
            System.out.println("play sound error: " + e.getMessage() + " for fail");
        }
    }
    
    public static void main(String[] args) {
        
        //Use the event dispatch thread for Swing components
        EventQueue.invokeLater(new Runnable(){

            public void run(){
                createAndShowGUI();                      
            }
        });  
        
        //as long as nobody said 'exit game' the game will go on
        while(changeItem() != 1){
        }
   }
}